# -*- coding:utf-8 -*-
# @Time  : 2019/11/22 20:06
# @Author: wangxb
import os
from threading import Thread
import time

import win32con
import win32gui
from pykeyboard import PyKeyboard
from pymouse import PyMouse

m = PyMouse()
k = PyKeyboard()


class JdguiThread(Thread):
    def __init__(self, thread_name, lanchon_tools_path):
        # 需要执行父类的初始化方法
        Thread.__init__(self)
        # 如果有参数，可以封装在类里面
        self.thread_name = thread_name
        # D:\Work\Python\GetWeChatID\dex-tools-lanchon
        self.lanchon_tools_path = lanchon_tools_path

    def run(self):
        print(
            "#######################Start jd-gui.exe classes-dex2jar.jar and save classes-dex2jar.src.zip######################")
        # 通过cmd调出jd-gui.exe工具
        os.system(
            self.lanchon_tools_path[0:1] + ": && cd " + self.lanchon_tools_path + " && jd-gui.exe classes-dex2jar.jar")


class KeyboardThread(Thread):
    def __init__(self, thread_name, jar_zip_file_path):
        Thread.__init__(self)
        self.thread_name = thread_name
        self.jar_zip_file_path = jar_zip_file_path

    def run(self):
        a = win32gui.FindWindow(None, "Java Decompiler - classes-dex2jar.jar")
        # print(a)
        # thread, processId = win32process.GetWindowThreadProcessId(a)
        loginid = win32gui.GetWindowPlacement(a)
        # print(loginid)
        m.click(loginid[4][0] + 300, loginid[4][1] + 260, 1, 1)

        ##另存为zip文件
        k.press_key(k.control_key)
        k.press_key(k.alt_key)
        k.tap_key("s")
        k.release_key(k.control_key)
        k.release_key(k.alt_key)
        time.sleep(1)

        # 替换zip包路径为GetWeChatID\zipSources
        # time.sleep(1)
        k.tap_key(k.tab_key, 6)
        # time.sleep(2)
        k.tap_key(k.enter_key)
        # time.sleep(1)
        k.type_string(self.jar_zip_file_path)
        # time.sleep(1)
        k.tap_key(k.enter_key)
        time.sleep(1)
        k.tap_key(k.tab_key, 9)
        # time.sleep(1)

        # 二次确定保存
        k.tap_key(k.enter_key)
        # 退出jd-gui.exe工具
        time.sleep(28)
        k.press_key(k.alt_key)
        k.tap_key("x")
        k.release_key(k.alt_key)
        print(
            "#######################Finished jd-gui.exe classes-dex2jar.jar and save classes-dex2jar.src.zip######################")
